package com.lwz.laboratory.controller;


import com.lwz.laboratory.dao.RegisterMapper;
import com.lwz.laboratory.entity.Register;
import com.lwz.laboratory.entity.User;
import com.lwz.laboratory.service.RegisterService;
import com.lwz.laboratory.service.TechnicianService;
import com.lwz.laboratory.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private TechnicianService technicianService;

    @Autowired
    private RegisterService registerService;
    
    @Autowired
    private UserService userService;




    

//    @RequestMapping(value = "/login", method = RequestMethod.POST)
//    public String checkStudent(@RequestParam(name = "stuid") String stuid, String password,ModelMap map) {
//        Register register = registerService.Login(stuid, password);
//        if (register != null) {
//            return "student_index";
//        } else {
//            return "login";
//
//        }
//    }
    @RequestMapping("/tostudentindex")
    public String tostudentindex(){
        return "student_index";
    }

    @RequestMapping("/totechnicianindex")
    public String totechniciantindex(){
        return "technician_index";
    }
    @RequestMapping("/tostudentworkindex")
    public String tostudentworktindex(){
        return "student_work_index";
    }
    @RequestMapping("/toassistantindex")
    public String toassistantindex(){
        return "assistant_index";
    }




    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String checkUser(@RequestParam(name = "userid") String userid, String password, ModelMap map, String rolename, HttpSession session, Model model) {


        User user = userService.checkUser(userid, password,rolename);

        if (user !=null &&rolename.equals("系统管理员")){
            session.setAttribute("USER_SESSION",user);
            return  "admin";

        }else if (user !=null &&rolename.equals("学生工作处")){
            session.setAttribute("USER_SESSION",user);
            return "student_work_index";
        }else if(user !=null &&rolename.equals("学生"))
        {
            session.setAttribute("USER_SESSION",user);
            return "student_index";
        }else if (user !=null &&rolename.equals("实验室助理")){
            session.setAttribute("USER_SESSION",user);
            return "assistant_index";

        }else if (user !=null &&rolename.equals("技术员")){
            session.setAttribute("USER_SESSION",user);
            return "technician_index";

        }
        map.addAttribute("msg","用户名或者密码错误");
        return "login";







    }

}
